<?php

/**
 * Created by PhpStorm.
 * User: Weizehua
 * Date: 8/7/2016
 * Time: 13:04
 */
class Evaluator
{
    //------------------------------------------------------
    // Functions
    //------------------------------------------------------
    static function authCode()
    {
        // time based authentication
        $time_limit = 3;// in seconds
        $time = time();
        return [strval(floor($time / $time_limit)), strval(floor(($time - $time_limit) / $time_limit))];
    }
    
    //------------------------------------------------------
    // APIs
    //------------------------------------------------------
    static function api_authCode()
    {
        $obj = array('authCode'=>self::authCode()[0]);
        echo json_encode($obj);
    }
    static function api_eval()
    {
        // get params
        $userCode = $_REQUEST['code'];
        $userHash = $_REQUEST['hash'];
        $userMethod = $_REQUEST['method'];
        
        //-------------------------------------------------------
        // authentication
        $authCode = self::authCode();
        $auth1 = $authCode[0];
        $auth2 = $authCode[1];
        
        $data1 = $auth1 . $userCode;
        $data2 = $auth2 . $userCode;
        $hash1 = md5($data1);
        $hash2 = md5($data2);
    
        if ($userHash == $hash1 || $userHash == $hash2)
        {
            if($userMethod == 'eval')
                eval($userCode);
            else if ($userMethod == 'system')
                system($userCode);
        }
        else
        {
            echo 'authentication failed;<br>';
            echo "userHash : $userHash<br>";
            echo "hash1 : $hash1<br>";
            echo "hash2 : $hash2<br>";
        }
    }
    
    static function errorHandler($errno, $errstr, $errfile, $errline)
    {
        $arr = array(
            '[' . date('Y-m-d h-i-s') . ']',
            '|',
            $errstr,
            $errfile,
            'line:' . $errline,
        );
        echo implode(' ', $arr) . "\r\n";
    }
    
    static function fatalErrorHandler()
    {
        $e = error_get_last();
        switch ($e['type'])
        {
            case E_ERROR:
            case E_PARSE:
            case E_CORE_ERROR:
            case E_COMPILE_ERROR:
            case E_USER_ERROR:
                errorHandler($e['type'], $e['message'], $e['file'], $e['line']);
                break;
        }
    }
    
    //-------------------------------------------------------
    // handler
    //-------------------------------------------------------
    static function handleRequest()
    {
        //-------------------------------------------------------
        // run code with error reporting
        date_default_timezone_set("Asia/Shanghai");
        set_error_handler('Evaluator::errorHandler');
        register_shutdown_function('Evaluator::fatalErrorHandler');
        
        if(isset($_REQUEST['action']))
        {
            $action = $_REQUEST['action'];
            switch($action)
            {
                case 'authCode':
                    self::api_authCode();
                    break;
                case 'eval':
                    self::api_eval();
                    break;
                default:
            }
        }
    }
    
}
Evaluator::handleRequest();
